<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区域 -->
      <SubFilter @filter-change="changeFilter" />

      <!-- 商品面板  （排序 + 列表） -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="sortChange"></SubSort>
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id">
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <!-- 无线加载组件 -->
        <XtxInfiniteLoading
          :loading="loading"
          :finished="finished"
          @infinite="getData"
        ></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from "./components/sub-bread";
import SubFilter from "./components/sub-filter";
import SubSort from "./components/sub-sort";
import GoodsItem from "./components/goods-item.vue";
import { ref, watch } from "vue";
import { findSubCategoryGoods } from "@/api/category";
import { useRoute } from "vue-router";
export default {
  name: "SubCategory",
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup() {
    const route = useRoute();
    const loading = ref(false);
    const finished = ref(false);
    // 商品列表数据
    const goodsList = ref([]);
    // 查询参数
    let reqParams = {
      page: 1,
      pageSize: 20,
    };
    const getData = () => {
      loading.value = true;
      // 发请求的时候要设置二级分类的 ID
      reqParams.categoryId = route.params.id;
      findSubCategoryGoods(reqParams).then(({ result }) => {
        // 获取数据成功~~
        // 判断数据有没有  没有 就是 没有数据了
        if (result.items.length) {
          goodsList.value.push(...result.items);
          // 改成下一页
          reqParams.page++;
          loading.value = false;
        } else {
          // 没有数据
          finished.value = true;
        }
        loading.value = false;
      });
      console.log("无限加载 --- 继续加载");
    };
    // 在更改了二级分类后 要重新加载
    watch(
      () => route.params.id,
      (newVal) => {
        if (newVal && route.path === "/category/sub/" + newVal) {
          finished.value = false;
          goodsList.value = [];
          reqParams = {
            page: 1,
            pageSize: 20,
          };
        }
      }
    );
    // 监听 排序 改变
    const sortChange = (sortParams) => {
      goodsList.value = [];
      finished.value = false;
      // 合并请求参数，要保留 之前的 筛选参数
      reqParams = { ...reqParams, ...sortParams };
      reqParams.page = 1;
    };

    // 监听筛选区改变
    const changeFilter = (filterParams) => {
      // console.log("筛选区域改变");
      goodsList.value = [];
      reqParams = { ...reqParams, ...filterParams };
      reqParams.page = 1;

      finished.value = false;
    };

    return { loading, finished, getData, goodsList, sortChange, changeFilter };
  },
};
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
